package com.inspur.common;

import java.io.Serializable;

public class ServerResponse<T>  implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code;
    private String desc;
    private T data;

    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
    private ServerResponse(int code, String desc, T data){
        this.code=code;
        this.data=data;
        this.desc=desc;
    };
    public boolean isSuccess(){
        return this.getCode()==ResultCode.SUCCESS.getCode();
    }
    public static <T> ServerResponse<T> getSuccess(){
        return new ServerResponse<T>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getDesc(),null);
    }
    public static <T> ServerResponse<T> getSuccessByData(T data){
        return new ServerResponse<T>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getDesc(),data);
    }
    public static <T> ServerResponse<T> getResponse(int code,String desc,T data){
        return new ServerResponse<T>(code,desc,data);
    }

    public static <T> ServerResponse<T> getResponseFail(int code,String desc){
        return new ServerResponse<T>(code,desc,null);
    }
    public static <T> ServerResponse<T> getResponseFailByDesc(String desc){
        return new ServerResponse<T>(ResultCode.OTHER_ERROR.getCode(),desc,null);
    }
    public static <T> ServerResponse<T> getResponseFail(ResultCode resultCode){
        return new ServerResponse<T>(resultCode.getCode(),resultCode.getDesc(),null);
    }
    public static <T> ServerResponse<T> getResponseFail(int code,String desc,T data){
        return new ServerResponse<T>(code,desc,data);
    }
    @Override
    public String toString() {
        return "ServerResponse{" +
                "code=" + code +
                ", desc='" + desc + '\'' +
                ", data=" + data +
                '}';
    }
}
